#ifndef __LIBS_H_
#define __LIBS_H_

#include "list.h"

#define MAX_CHAPTER_NAME_LEN 1024
struct toc_link {
	struct list_head list; //linux list

	int chapter; //chapter index, 0-based
	int level; //0 is top level; 1 is second level
	char chapter_name[MAX_CHAPTER_NAME_LEN]; //utf8
	int start_page; //1 based
	int end_page; //1 based
	//init to total pages number of this PDF
	//finally: num_pages = end_page - start_page + 1
	int num_pages;
};

extern void init_list(struct toc_link* head, int total_pages);

/*
* attach to tail
* name: title name
* start: start page index
* level: 0 is top level; 1 is second level
*/
extern void insert_list(struct toc_link* head, char* name, int start, int level);

extern void destroy_list(struct toc_link* head);


///// os part ////////////////////
//printf UTF-8 string to stdout
extern void printf_utf8(char* buf);

//u16 to utf8. call free(out) after use
extern char* u16_to_u8(char* in, int* out_len);

//utf_content = to_utf(buf, &realsize);
//gb2312 to utf8, should free memory by caller
extern char* to_utf(char* in, int* out_len);

extern void get_filename(char* path, char* filename);

///// pdf lib part ////////////////////
extern void pdflib_init(void);
#define pdflib_exit FPDF_DestroyLibrary

//open PDF document, return NULL if error
extern FPDF_DOCUMENT pdflib_open_doc(const char* path);
#define pdflib_close_doc FPDF_CloseDocument

//get table of content, and build toc list
//level = 0 : top toc, pdflib_toc(doc, NULL, 0)
//level = 1 : second toc, pdflib_toc(doc, toc, 1)
extern void pdflib_toc(FPDF_DOCUMENT doc, FPDF_BOOKMARK toc, int level, struct toc_link* head);

//page_number: 1 based
//return 0 if OK
//free(out) after use
extern char* pdflib_page_text(FPDF_DOCUMENT doc, int page_number, int* out_len);

//reformat
extern void reformat_text(FILE* fp, char* buf);

//pdf to markdown
extern int output_to_md(FPDF_DOCUMENT doc, FILE* fp, struct toc_link* head);

#endif